﻿using System;
using System.Linq;
using System.Windows.Forms;
using MovieBrowser.Model;

namespace MovieBrowser.Presentation.Forms.Db
{
    public partial class MovieForm : Form
    {
        private readonly MovieDbEntities _entities;
        private readonly MovieSearchForm _movieSearchForm = new MovieSearchForm();

        public MovieForm()
        {
            InitializeComponent();
            _entities = new MovieDbEntities();
        }

        private void MovieFormLoad(object sender, EventArgs e)
        {
            movieBindingSource.DataSource = _entities.Movies;
        }

        private void MovieBindingNavigatorSaveItemClick(object sender, EventArgs e)
        {
            _entities.SaveChanges();
        }
        
        private void ToolStripButtonFindClick(object sender, EventArgs e)
        {
            _movieSearchForm.ShowDialog(this);
            var ds = _entities.Movies.Select(o => o);
            if (!string.IsNullOrEmpty(_movieSearchForm.MovieTitle))
                ds = ds.Where(o => o.Title.ToLower().Contains(_movieSearchForm.MovieTitle.ToLower()));
            if (!string.IsNullOrEmpty(_movieSearchForm.MovieId))
                ds = ds.Where(o => o.ImdbId.Contains(_movieSearchForm.MovieId));
            movieBindingSource.DataSource = ds;
        }


    }
}
